﻿using System;
public static class GlobalMembersLan_battle
{

//	internal static LANBattleRoom_ my_lan_battle_room = new LANBattleRoom_();
//	internal static LCUI_Thread lan_battle_host_th = 0;
//	internal static LCUI_Thread lan_battle_scan_th = 0;
//	internal static LCUI_Queue lan_battle_room_list = new LCUI_Queue();

//	// 负责广播当前计算机创建的房间信息 
//	internal static void LANBattle_HostRoomThread(IntPtr arg)
//	{
//		SOCKET sockListener = new SOCKET();
//		int fBroadcast = DefineConstantsLan_battle.TRUE;
//		SOCKADDR_IN saUdpServ = new SOCKADDR_IN();

//		sockListener = socket(PF_INET, SOCK_DGRAM, 0);
//		setsockopt(sockListener, SOL_SOCKET, SO_BROADCAST, (string)&fBroadcast, typeof(int));
//		saUdpServ.sin_family = AF_INET;
//		saUdpServ.sin_addr.s_addr = inet_addr("255.255.255.255");
//		saUdpServ.sin_port = htons(DefineConstantsLan_battle.UDP_PORT_NUM);

//		while(1)
//		{
//			sendto(sockListener, (string) my_lan_battle_room, typeof(LANBattleRoom_), 0, (SOCKADDR) saUdpServ, typeof(SOCKADDR_IN));
//			DEBUG_MSG1("[server] room name: %s, host name: %s, host ip: %s\n", my_lan_battle_room.name, my_lan_battle_room.host.name, my_lan_battle_room.host.ip_addr);
//			LCUI_MSleep(10);
//		}
//		_LCUIThread_Exit(null);
//		return;
//	}

//	internal static void RoomList_Update()
//	{
//		int i;
//		int n;
////C++ TO C# CONVERTER TODO TASK: Pointer arithmetic is detected on this variable, so pointers on this variable are left unchanged.
//		LANBattleRoomRec_ *room_rec = new LANBattleRoomRec_();
//		LCUI_Queue room_list;

//		room_list = lan_battle_room_list;
//		Queue_Lock(room_list);
//		n = Queue_GetTotal(room_list);
//		for(i =0; i<n; ++i)
//		{
//			room_rec = (LANBattleRoomRec_)Queue_Get(room_list, i);
//			if (room_rec == null)
//			{
//				continue;
//			}
//			++room_rec.unexist_count;
//			if(room_rec.unexist_count >= 2 *n)
//			{
//				Queue_Delete(room_list, i);
//				n = Queue_GetTotal(room_list);
//				--i;
//			}
//		}
//		Queue_Unlock(room_list);
//	}

//	// 标记一个房间已存在，若房间列表中无该房间记录，则添加它 
//	internal static void RoomList_MarkExist(LANBattleRoom_ room)
//	{
//		int i;
//		int n;
//		LANBattleRoomRec_ room_buff = new LANBattleRoomRec_();
//		LANBattleRoomRec_ room_rec;
//		LCUI_Queue room_list;

//		room_list = lan_battle_room_list;
//		Queue_Lock(room_list);
//		n = Queue_GetTotal(room_list);
//		for(i =0; i<n; ++n)
//		{
//			room_rec = (LANBattleRoomRec_)Queue_Get(room_list, i);
//			if (room_rec == null)
//			{
//				continue;
//			}
//			if(string.Compare(room.name, room_rec.room.name) == 0 && string.Compare(room.host.ip_addr, room_rec.room.host.ip_addr) == 0)
//			{
//				 room_rec.unexist_count = 0;
//				 break;
//			}
//		}
//		if(i >= n)
//		{
//			room_buff.unexist_count = 0;
////C++ TO C# CONVERTER TODO TASK: The memory management function 'memcpy' has no equivalent in C#:
//			memcpy(room_buff.room, room, typeof(LANBattleRoom_));
//			Queue_Add(room_list, room_buff);
//		}
//		Queue_Unlock(room_list);
//	}

//	// 负责扫描当前局域网中的各个房间信息 
//	internal static void LANBattle_ScanRoomThread(IntPtr arg)
//	{
//		int nSize;
//		int nbSize;
//		int timeout;
//		LANBattleRoom_ room = new LANBattleRoom_();
//		SOCKET sockListener = new SOCKET();
//		SOCKADDR_IN sin = new SOCKADDR_IN();
//		SOCKADDR_IN saClient = new SOCKADDR_IN();

//		sockListener = socket(AF_INET, SOCK_DGRAM,0);
//		sin.sin_family = AF_INET;
//		sin.sin_port = htons(DefineConstantsLan_battle.UDP_PORT_NUM);
//		sin.sin_addr.s_addr = htonl(INADDR_ANY);

//		timeout = 10;
//		setsockopt(sockListener, SOL_SOCKET, SO_RCVTIMEO, (string)&timeout, typeof(timeout));

//		if(bind(sockListener, (SOCKADDR FAR *) sin, typeof(sin))!=0)
//		{
//			Console.Write("Can't bind socket to local port!Program stop.\n");
//			closesocket(sockListener);
//			LCUIThread_Exit(null);
//			return;
//		}

//		while(1)
//		{
//			nSize = typeof(SOCKADDR_IN);
//			nbSize = recvfrom(sockListener, (string) room, typeof(LANBattleRoom_), 0, (SOCKADDR FAR *) saClient, nSize);
//			// 更新房间列表 
//			RoomList_Update();
//			if(nbSize == SOCKET_ERROR)
//			{
//				LCUI_MSleep(20);
//				continue;
//			}
//			// 标记该房间存在 
//			RoomList_MarkExist(room);
//		}
//		closesocket(sockListener);
//		_LCUIThread_Exit(null);
//		return;
//	}

//	public static int LANBattle_Init()
//	{
//		WSADATA wsaData = new WSADATA();
//		if(WSAStartup(MAKEWORD(1, 1), wsaData) !=0)
//		{
//			Console.Write("Can't initiates windows socket! Program stop.\n");
//			return -1;
//		}
//		Queue_Init(lan_battle_room_list, typeof(LANBattleRoomRec_), null);
//		return 0;
//	}

//	public static int LANBattle_Exit()
//	{
//		return 0;
//	}

//	// 开始扫描对战房间 
//	public static void LANBattleRoom_StartScan()
//	{
//		_LCUIThread_Create(lan_battle_scan_th, LANBattle_ScanRoomThread, null);
//	}

//	// 创建房间 
//	public static int LANBattleRoom_Create(string room_name)
//	{
//		hostent host_ptr;
//		LANBattleRoom_ p_room;
//		in_addr addr = new in_addr();

//		if(lan_battle_host_th != 0)
//		{
//			return 0;
//		}
//		p_room = my_lan_battle_room;
//		host_ptr = gethostbyname("");
//		addr = *(in_addr) host_ptr.h_addr_list;
//		// 保存本地主机名 
//		p_room.host.name = host_ptr.h_name;
//		// 保存本地主机ip地址 
//		p_room.host.ip_addr = inet_ntoa(addr);
//		// 保持房间名称 
//		p_room.name = room_name;
//		// 创建线程，以广播房间信息 
//		return _LCUIThread_Create(lan_battle_host_th, LANBattle_HostRoomThread, null);
//	}

//	// 退出房间 
//	public static int LANBattleRoom_Exit()
//	{
//		return 0;
//	}

//	public static void LANBattleRoom_GetList(ref LCUI_Queue out_room_list)
//	{
//		int i;
//		int n;
//		LANBattleRoomRec_ room_rec;
//		LCUI_Queue room_list;

//		// 清空房间列表 
//		while(Queue_Delete(out_room_list, 0));
		
//		room_list = lan_battle_room_list;
//		Queue_Lock(room_list);
//		n = Queue_GetTotal(room_list);
//		for(i =0; i<n; ++i)
//		{
//			room_rec = (LANBattleRoomRec_)Queue_Get(room_list, i);
//			if (room_rec == null)
//			{
//				continue;
//			}
//			Queue_Add(out_room_list, room_rec.room);
//		}
//		Queue_Unlock(room_list);
//	}


//	public static void RoomList_Init(ref LCUI_Queue list)
//	{
//		Queue_Init(list, typeof(LANBattleRoom_), null);
//	}

//	// 加入房间 
//	public static int LANBattleRoom_Join(LANBattleRoom_ room_buff)
//	{
//		return 0;
//	}
//	//----------------------------- END LANBattleRoom ---------------------------


//	internal static  LCUI_Widget_ *btn_create_room = new ();
//	internal static  btn_join_room;

//	// 初始化局域网对战 
//	public static void Game_InitLANBattle()
//	{
//		LANBattle_Init();

//		btn_create_room = Widget_New("button");
//		btn_join_room = Widget_New("button");

//		Widget_SetAutoSize(btn_create_room, DefineConstantsLan_battle.FALSE, 0);
//		Widget_SetAutoSize(btn_join_room, DefineConstantsLan_battle.FALSE, 0);
//		Widget_Resize(btn_create_room, Size(100,30));
//		Widget_Resize(btn_join_room, Size(100,30));
//		Button_TextW(btn_create_room, "创建房间");
//		Button_TextW(btn_join_room, "加入房间");
//		Widget_SetAlign(btn_create_room, ALIGN_TYPE_.ALIGN_BOTTOM_RIGHT, Pos(-10,-10));
//		Widget_SetAlign(btn_join_room, ALIGN_TYPE_.ALIGN_BOTTOM_RIGHT, Pos(-120,-10));
//		Widget_Show(btn_create_room);
//		Widget_Show(btn_join_room);
//	}
}

//C++ TO C# CONVERTER WARNING: The following #include directive was ignored:
//#include <winsock.h>

//------------------------------ LANBattleRoom ----------------------------
//C++ TO C# CONVERTER TODO TASK: There is no equivalent to most C++ 'pragma' directives in C#:
//#pragma comment(lib,"ws2_32.lib")

public class LANBattleUserInfo_
{
	public string name = new string(new char[256]);
	public string ip_addr = new string(new char[13]);
}

public class LANBattleRoom_
{
	public string name = new string(new char[32]); //*< 房间名 
	public LANBattleUserInfo_ host = new LANBattleUserInfo_(); //*< 房主的信息 
	public LANBattleUserInfo_ other = new LANBattleUserInfo_(); //*< 参与者的信息 
	public LCUI_BOOL connected = new LCUI_BOOL(); //*< 参与者是否与房主建立了连接 
}

public class LANBattleRoomRec_
{
	public LANBattleRoom_ room = new LANBattleRoom_(); //*< 房间信息 
	public int unexist_count; //*< 当前时段内房间不存在的次数 
}